package com.woniuxy.lab.woniu_lab_boss.Security.comment;



import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.lab.woniu_lab_boss.model.Account;
import com.woniuxy.lab.woniu_lab_boss.model.Admin;
import com.woniuxy.lab.woniu_lab_boss.model.Teacher;
import com.woniuxy.lab.woniu_lab_boss.service.AccountService;
import com.woniuxy.lab.woniu_lab_boss.service.AdminService;
import com.woniuxy.lab.woniu_lab_boss.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 17292
 */
@Slf4j
@Component
public class LoadUserServiceImpl implements UserDetailsService {

    public static final String TEACHER = "TEACHER";
    public static final String USERNAME = "username";
    public static final String STATUS = "status";
    public static final String ENABLE = "ENABLE";
    public static final String ADMIN = "ADMIN";
    @Resource
    private AccountService accountService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private AdminService adminService;


    /**
     *
     * @param username 用户名
     * @return UserDetails
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Account account = accountService.getOne(new QueryWrapper<Account>()
                .eq(USERNAME, username));

        if (account == null) {
            throw new UsernameNotFoundException("该账号不存在或被禁用");
        }
        //讲师登录校验
        if(account.getType().equals(TEACHER)){
            Teacher teacher = teacherService.getOne(new QueryWrapper<Teacher>().eq(USERNAME, username)
                    .eq(STATUS, ENABLE));
            if (teacher==null){
                throw new UsernameNotFoundException("该讲师账号不存在或被禁用");
            }
        }
        //管理员登录校验
        if(account.getType().equals(ADMIN)){
            Admin admin = adminService.getOne(new QueryWrapper<Admin>().eq(USERNAME, username)
                    .eq(STATUS, ENABLE));
            if (admin==null){
                throw new UsernameNotFoundException("该管理员账号不存在或被禁用");
            }
        }

//        Integer personId = account.getPersonId();
//        if (account.getType().equals(TEACHER)){
//            Teacher teacher = teacherService.getById(personId);
//        }


        LabAccountDetail bookAccountDetail = new LabAccountDetail();
        LabAccountDetail labAccountDetail = BeanUtil.copyProperties(account, LabAccountDetail.class);
//        bookAccountDetail.set

//        //查询角色
//        Role role = accountService.getRoleByAccountId(accountId);
//        bookAccountDetail.setAuthorities(AuthorityUtils.createAuthorityList("ROLE_" + role.getRoleName()));
//        log.info("role名字：{}", role.getRoleName());
//        //查询资源
//        List<String> resources = accountService.getResourceByAccountId(accountId);
//        resources.add("ROLE_" + role.getRoleName());
//        String[] strings = resources.toArray(new String[resources.size()]);
//
//        //循环插入资源
//        log.info("权限及角色数组:{}", strings);
//        bookAccountDetail.setAuthorities(AuthorityUtils.createAuthorityList(strings));


        return labAccountDetail;
    }


}
